<?php
	/**
	* Products View
	* 
	* @author trungdt
	* @since Oct 14, 2011
	*/
	if (!defined('EXPONENT')) exit('');

	$product = null;
	if (isset($_REQUEST['name'])) {
		$seo_name = _ab_safe_html_string($_REQUEST['name']);
		$product = $db->selectObject('productsmodule_products',"seo_name = '{$seo_name}'");
		if ($product) {

			// update count view
			$product->count_view++;
			$sql="UPDATE {$db->prefix}productsmodule_products SET count_view={$product->count_view} WHERE {$db->prefix}productsmodule_products.id={$product->id}";
			$db->sql($sql);

			$loc = unserialize($product->location_data);
			$config=$db->selectObject("productsmodule_config", "location_data = '".serialize($loc)."'");

			$price_max=0; $price_min=0;

			$brand = productsmodule::__GetBrandByID($product->brand_id);
			$product=productsmodule::__ProductProcess($product, $config, $price_max, $price_min,false );
			$product=$product[0];

			// accessories, must be loaded manual
			$accessories_temp=explode(";",$product->accessories);
			$accessories_where="";
			foreach ($accessories_temp as $vk=>$vv)
				if ($vv!="")
				{
					if ($accessories_where!= "")
						$accessories_where.=" OR ";
					$accessories_where.="(id = {$vv})";
				}
			if ($accessories_where != "")
			{
				$accessories=$db->selectObjects("productsmodule_products", $accessories_where);
				$accessories=productsmodule::__ProductProcess($accessories, $config, $price_max, $price_min);
			}
			else
				$accessories=array();
				

			// similar products
			$temp=serialize($loc);
			$product_similar=$db->selectObjects("productsmodule_products", "location_data = '{$temp}' AND (public_state = 1) AND (cat_id = {$product->cat_id}) AND (id != {$product->id})  ORDER BY post_date DESC, stars DESC LIMIT 0, 12");
			$product_similar = productsmodule::__ProductProcess($product_similar, $config,$price_max, $price_min, false );
			// same brand
			$product_same_brand=$db->selectObjects("productsmodule_products", "location_data = '{$temp}' AND (public_state = 1) AND (brand_id = {$product->brand_id}) AND (id != {$product->id}) ORDER BY post_date DESC, stars DESC LIMIT 0, 12");
			$product_same_brand = productsmodule::__ProductProcess($product_same_brand, $config,$price_max, $price_min, false );

			// fetch to template
			$template=new template("productsmodule","_ProductView",$loc);
			$template->register_permissions(array('administrate',"configure", "create", "edit", "delete"),$loc);
			$template->assign("config", $config);
			$template->assign("product", $product);
			$template->assign("brand", $brand);

			$template->assign("breadcum", productsmodule::__PDoremiShop_CategorySeoNameBuild($product->cat_id, 1));            // set page title

			$template->assign("product_similar", $product_similar);
			$template->assign("product_similar_count", count($product_similar));

			$template->assign("product_same_brand", $product_same_brand);
			$template->assign("product_same_brand_count", count($product_same_brand));

			$template->assign("product_asscessory_count", count($accessories));
			$template->assign("product_asscessory", $accessories);

			_ab_theme_action_register_title($product->title);
			_ab_theme_action_register_title($product->search_des,1,0);


			$template->output();
		}
		else
			echo SITE_404_HTML;
	}

?>
